package com.tttare.haoyigou.sellers.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.tttare.haoyigou.mapper.TbSpecificationMapper;
import com.tttare.haoyigou.mapper.TbSpecificationOptionMapper;
import com.tttare.haoyigou.pojo.TbBrand;
import com.tttare.haoyigou.pojo.TbBrandExample;
import com.tttare.haoyigou.pojo.TbSpecification;
import com.tttare.haoyigou.pojo.TbSpecificationExample;
import com.tttare.haoyigou.pojo.TbSpecificationOption;
import com.tttare.haoyigou.pojo.TbSpecificationOptionExample;
import com.tttare.haoyigou.pojo.TbSpecificationOptionExample.Criteria;
import com.tttare.haoyigou.pojogroup.Specification;
import com.tttare.haoyigou.sellers.service.SpecificationService;

import entity.PageResult;
@Service
public class SpecificationServiceImpl implements SpecificationService {

	@Autowired
	private TbSpecificationMapper tbSpecificationMapper;
	
	@Autowired
	private TbSpecificationOptionMapper tbSpecificationOptionMapper;
	
	@Override
	public List<TbSpecification> findAll() {
		List<TbSpecification> list = tbSpecificationMapper.selectByExample(null);
		return list;
	}

	@Override
	public PageResult<TbSpecification> findPage(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		Page<TbSpecification> pageContent = (Page<TbSpecification>)tbSpecificationMapper.selectByExample(null);
		PageResult<TbSpecification> result = new PageResult<TbSpecification>(
				pageContent.getTotal(),pageContent.getResult());
		return result;
	}

	@Override
	public void add(Specification specification) {
		TbSpecification tbSpecification = specification.getSpecification();
		//一定记住设置selectKey,返回主键
		tbSpecificationMapper.insert(tbSpecification);
		//获得SpecifiactionOption(注意思路转变,使用mybatis,
		//我们将多对多,一对多的关联写在service层,用java代码实现,不需要考虑数据库多表关联的种种操作)
		List<TbSpecificationOption> specificationOptionList = specification.getSpecificationOptionList();
		for (TbSpecificationOption tbSpecificationOption : specificationOptionList) {
			tbSpecificationOption.setSpecId(tbSpecification.getId());
			tbSpecificationOptionMapper.insert(tbSpecificationOption);
		}
	}

	@Override
	public void update(Specification specification) {
		TbSpecification tbSpecification = specification.getSpecification();
		tbSpecificationMapper.updateByPrimaryKey(tbSpecification);
		//tbSpecificationMapper包含的Option修改,因为这个修改既可以是Option数量的修改,也可以是Option数据的修改,
		//逻辑较为复杂,故这个部分,不使用Update,而是先删除再新增
		//创建条件
		TbSpecificationOptionExample example = new TbSpecificationOptionExample();
		Criteria criteria = example.createCriteria();
		criteria.andSpecIdEqualTo(tbSpecification.getId());
		//删除option
		tbSpecificationOptionMapper.deleteByExample(example);
		//新增option
		List<TbSpecificationOption> specificationOptionList = specification.getSpecificationOptionList();
		for (TbSpecificationOption tbSpecificationOption : specificationOptionList) {
			tbSpecificationOptionMapper.insert(tbSpecificationOption);
		}
	}

	@Override
	public Specification findOne(Long id) {
		TbSpecification tbSpecification = tbSpecificationMapper.selectByPrimaryKey(id);
		TbSpecificationOptionExample example = new TbSpecificationOptionExample();
		Criteria criteria = example.createCriteria();
		criteria.andSpecIdEqualTo(id);
		List<TbSpecificationOption> list = tbSpecificationOptionMapper.selectByExample(example);
		Specification specification = new Specification(tbSpecification,list);
		return specification;
	}

	@Override
	public void delete(Long[] ids) {
		for (Long id : ids) {
			//删除specification
			tbSpecificationMapper.deleteByPrimaryKey(id);
			//删除option
			TbSpecificationOptionExample example = new TbSpecificationOptionExample();
			Criteria criteria = example.createCriteria();
			criteria.andSpecIdEqualTo(id);
			tbSpecificationOptionMapper.deleteByExample(example);
		}

	}

	@Override
	public PageResult findPage(TbSpecification specification, int pageNum,
			int pageSize) {
		//先分页
		PageHelper.startPage(pageNum, pageSize);
		//如果TbBrand不为null就条件查询(Example是专门用来条件查询的类)
		TbSpecificationExample example = new TbSpecificationExample();
		com.tttare.haoyigou.pojo.TbSpecificationExample.Criteria criteria = example.createCriteria();
		if(specification!=null){
			if(specification.getSpecName()!=null&&StringUtils.isNotBlank(specification.getSpecName())){
				criteria.andSpecNameLike("%"+specification.getSpecName()+"%");
			}
		}
		Page<TbSpecification> pageContent = (Page<TbSpecification>)tbSpecificationMapper.selectByExample(example);
		//封装数据在PageResult中
		PageResult<TbSpecification> pageResult = new PageResult<TbSpecification>();
		pageResult.setTotal(pageContent.getTotal());
		pageResult.setRows(pageContent.getResult());
		return pageResult;
	}

	@Override
	public List<Map> selectOptionList() {
		List<Map> list = tbSpecificationMapper.selectOptionList();
		return list;
	}

}
